// pages/member/integrallist/integrallist.js
var app = getApp();
const skinBehavior = require("../../../utils/themeSkin/skinBehavior");
Page({
  behaviors: [skinBehavior],
  /**
   * 页面的初始数据
   */
  data: {
    selected: 0,
    all: [],
    //收入积分
    income: [],
    //消费积分
    out: [],
    integral: '积分',
    statusValue: 0,
    selectShow: false,
    selectType: 0,
    record_list: [],
    money_arr: [],
    currentDate: new Date().getTime(),
    maxDate: new Date().getTime(),
    show: false,
    postDate: "",
    formatter(type, val) {
      if (type === 'year') {
        return `${val}年`;
      } else if (type === 'month') {
        return `${val}月`;
      }
      return val;
    },
    noneShow: true,
    // 业务类型数组
    source_comment: [],
    selectTapInd: 0,


    //more
    isLoadMore: true,
    page: 1,
    total_page: 0,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getData();
    try {
      const value = wx.getStorageSync('integral');
      if (value) {
        this.setData({
          integral: value
        });
        // Do something with return value
        if (this.data.integral) {
          wx.setNavigationBarTitle({
            title: this.data.integral + '列表'
          });
        }
      }
    } catch (e) {
      // Do something when catch error
    }
  },
  selectShowTap() {
    this.setData({
      selectShow: true
    })
  },
  selectStatus(e) {
    let key = e.currentTarget.dataset.key;
    this.init();
    this.setData({
      statusValue:key
    })
    this.getData();
  },
  pickConfirm(e){
    let time = this.timestampToTime(e.detail);
    this.setData({
      postDate:time,
      show:false
    })
    this.init();
    this.getData();
  },
  showTap() {
    this.setData({
      show: true,
    });
  },
  cancel(){
    this.setData({
      show:false
    })
  },
  timestampToTime(timestamp, key) {
    if (!Number(timestamp) || timestamp == "") {
      return "";
    }
    let date = new Date(timestamp); //时间戳为10位需*1000，时间戳为13位的话不需乘1000
    console.log(date,'ddddd1')
    let Y = date.getFullYear() + "-";
    let M = (date.getMonth() + 1 < 10 ? "0" + (date.getMonth() + 1) : date.getMonth() + 1) + "-";
    let D = date.getDate() < 10 ? "0" + date.getDate() : date.getDate();
    return Y + M + D;
  },
  selectTap(e) {
    let key = e.currentTarget.dataset.ind;
    this.init();
    this.setData({
      selectType: key,
      selectTapInd: key,
      selectShow:false
    })
    this.getData();
  },
  init() {
    this.setData({
      isLoadMore: true,
      page: 1,
      total_page: 0,
      record_list: [],
      money_arr: [],
    });
  },
  tapIs(e) {
    let item = e.currentTarget.dataset.item;
    wx.navigateTo({
      url: '/packageB/member/integralDetail/integralDetail' + '?id=' + item.id,
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this.getMoreData();
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  getData() {
    let json = {
      status: this.data.statusValue == 2 ? '-1' : this.data.statusValue,
      month: this.data.postDate,
      mode: this.data.selectType,
    };
    this.data.page = 1;
    let urlStr = app.getNetAddresss("finance.point-info");
    app._postNetWork({
      url: urlStr,
      data: json,
      success: (resdata) => {
        var res = resdata.data;
        if (res.result == 1) {
          this.data.isLoadMore = true;
          if (res.data.source_comment) {
            res.data.source_comment[0] = '变动类型';
            this.setData({
              source_comment: res.data.source_comment,
            })
          }
          this.setData({
            total_page: res.data.record_list.last_page,
            money_arr: res.data.point_arr,
            record_list: res.data.record_list,
            noneShow: false,
          });
          wx.setNavigationBarColor({
            frontColor: '#ffffff',
            backgroundColor: this.data.themeBaseColor,
            animation: {
              duration: 400,
              timingFunc: 'easeIn'
            }
          });
        } else {
          this.setData({
            noneShow: true,
            isLoadMore: false,
            record_list: []
          })
        }
      },
      fail: function (res) {
        console.log(res);
      },
    });
  },
  getMoreData() {
    this.data.isLoadMore = false
    if (this.data.page >= this.data.total_page) {
      return;
    } else {
      this.data.page = this.data.page + 1;
      let json = {
        status: this.data.statusValue == 2 ? '-1' : this.data.statusValue,
        month: this.data.postDate,
        mode: this.data.selectType,
        page: this.data.page,
      };
      let urlStr = app.getNetAddresss("finance.point-info");
      app._postNetWork({
        url: urlStr,
        data: json,
        success: (resdata) => {
          var response = resdata.data;
          if (response.result == 1) {
            var nextPageData = response.data.record_list.data;
            for (const key in nextPageData) {
              if (this.data.record_list.data[key]) {
                this.data.record_list.data[key] = [...this.data.record_list.data[key], ...nextPageData[key]]
              } else {
                this.data.record_list.data[key] = nextPageData[key]
              }
            }
            this.setData({
              record_list: this.data.record_list,
              money_arr: Object.assign(this.data.money_arr, response.data.point_arr)
            })
            this.isLoadMore = true;
          } else {
            this.setData({
              page: this.data.page - 1,
              isLoadMore: false,
            });
          }
        },
        fail: function (res) {
          console.log(res);
        },
      });
    }
  },
  swichTabTItem(e) {
    let idx = e.detail.index;
    if (idx == 0) {
      this.setData({
        selected: '',
        recordsList: []
      });
      this._getIntegralInfo('');
    } else if (idx == 1) {
      this.setData({
        selected: 1,
        recordsList: []
      });
      this._getIntegralInfo(1);
    }
    if (idx == 2) {
      this.setData({
        selected: -1,
        recordsList: []
      });
      this._getIntegralInfo(-1);
    }
  }
});